/**
 * Created by yu on 16/9/3.
 * jsonp server
 */
var http = require('http');
var fs = require('fs');
var url = require('url');

var server = http.createServer(function(req,res){
    var urlObj = url.parse(req.url,true);
    var pathname = urlObj.pathname;
    if(pathname == '/'){
        fs.readFile('./index.html',function(err,data){
            res.setHeader('Content-Type','text/html;charset=utf-8');
            res.end(data.toString());
        })
    }else if(pathname=='/books'){
        var query = urlObj.query;
        var returnStr = query.callback+'(["'+query.name+'"])';
        console.log(returnStr);
        res.end(returnStr);
    }
}).listen(8080,'localhost');
